﻿using LdtJudge.DatabaseCore.Interfac;
using LdtJudge.DataEngine.CallInterface.CommHelp;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Model.DataModel.SystemtableModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace LdtJudge.DataEngine.Data
{
    /// <summary>
    /// 数据访问类:DALSYS_Express_MatchSituation
    /// </summary>
    public class DALSYS_Express_MatchSituation
    {
        // private static string connsys = ConfigRetrieval.GetConfigRetrieval.GetConnectionString("Ldt_JudgeDocSystem");
        private static IFreeSql freeSql = FreeSQLHelp.GetFreeSQL("Ldt_JudgeDocSystem");
        //private static ISqlHelper sqlSysexpress;
        public DALSYS_Express_MatchSituation()
        {
          ///  sqlSysexpress = new Data.SqlHandle(connsys).GetSqlHelper;
        }
        #region  Method
       
        /// <summary>
        /// Gets the model.
        /// ljx 做的扩展 2017-11-07 修改
        /// 专门针对组合情形
        /// </summary>
        /// <param name="SituationID">The situation identifier.</param>
        /// <param name="IsMatch">The is match.</param>
        /// <param name="MatchSource">The match source.</param>
        /// <param name="AffirmIdeaSource">The affirm idea source.</param>
        /// <param name="AffirmIdeaResult">The affirm idea result.</param>
        /// <param name="ParentID">The parent identifier.</param>
        /// <param name="AllStituationIDs">All stituation i ds.</param>
        /// <returns>MSYS_Express_MatchSituation.</returns>
        public Model.DataModel.SysCommon.MSYS_Express_MatchSituation GetModel(string SituationID, int IsMatch, string AffirmIdeaResult, List<string> AllStituationIDs)
        {
           var table=  freeSql.Select<SYS_Express_MatchSituation>().Where(a => a.AllParentID != "-1" && a.CC_IsShowExpress==1 && a.IsMatch==IsMatch && a.AffirmIdeaResult==AffirmIdeaResult && AllStituationIDs.Contains(a.SituationID)).ToList();
            if (table.Count>0)
            {
                foreach (var tablevalue in table)
                {
                    List<string> strlist = tablevalue.AllStituationIDs.Split(',').ToList();
                    if (AllStituationIDs.Count == strlist.Count && AllStituationIDs.Except(strlist).ToList().Count == 0)
                    {
                        return CopySameFieldsObject<Model.DataModel.SysCommon.MSYS_Express_MatchSituation>(tablevalue);
                    }
                }
                
            }
            return null;
        }
        /// <summary>
        /// 转换两个不同类型但是成员相同的对象
        /// </summary>
        /// <typeparam name="T">目标对象</typeparam>
        /// <param name="source">待转换对象</param>
        /// <returns></returns>
        public static T CopySameFieldsObject<T>(Object source)
        {
            return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source));
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.DataModel.SysCommon.MSYS_Express_MatchSituation GetModel(string SituationID, int IsMatch, string MatchSource, string AffirmIdeaSource, string AffirmIdeaResult, string ParentID)
        {
          
            var table= freeSql.Select<SYS_Express_MatchSituation>().Where(a => a.SituationID == SituationID && a.IsMatch == IsMatch && a.AffirmIdeaResult == AffirmIdeaResult && a.AllStituationIDs == SituationID).ToList();
            if (table.Count>0)
            {
                return CopySameFieldsObject<Model.DataModel.SysCommon.MSYS_Express_MatchSituation>(table[0]);
            }
            return null;
        }
       

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.DataModel.SysCommon.MSYS_Express_MatchSituation DataRowToModel(DataRow row)
        {
            Model.DataModel.SysCommon.MSYS_Express_MatchSituation model = new Model.DataModel.SysCommon.MSYS_Express_MatchSituation();
            if (row != null)
            {
                if (row["MS_ID"] != null)
                {
                    model.MS_ID = row["MS_ID"].ToString();
                }
                if (row["CauseGroupID"] != null)
                {
                    model.CauseGroupID = row["CauseGroupID"].ToString();
                }
                if (row["SituationID"] != null)
                {
                    model.SituationID = row["SituationID"].ToString();
                }
                if (row["IsMatch"] != null && row["IsMatch"].ToString() != "")
                {
                    model.IsMatch = int.Parse(row["IsMatch"].ToString());
                }
                if (row["MatchSource"] != null)
                {
                    model.MatchSource = row["MatchSource"].ToString();
                }
                if (row["AffirmIdeaSource"] != null)
                {
                    model.AffirmIdeaSource = row["AffirmIdeaSource"].ToString();
                }
                if (row["AffirmIdeaResult"] != null)
                {
                    model.AffirmIdeaResult = row["AffirmIdeaResult"].ToString();
                }
                
                if (row["SituationType"] != null && row["SituationType"].ToString() != "")
                {
                    model.SituationType = int.Parse(row["SituationType"].ToString());
                }
                if (row["ParentID"] != null)
                {
                    model.ParentID = row["ParentID"].ToString();
                }
                if (row["allParentID"] != null)
                {
                    model.AllParentID = row["allParentID"].ToString();
                }
                if (row["AllStituationIDs"] != null)
                {
                    model.AllStituationIDs = row["AllStituationIDs"].ToString();
                }
            }
            return model;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.DataModel.SysCommon.MSYS_Express_MatchSituation ReaderBind(IDataReader dataReader)
        {
            Model.DataModel.SysCommon.MSYS_Express_MatchSituation model = new Model.DataModel.SysCommon.MSYS_Express_MatchSituation();
            object ojb;
            model.MS_ID = dataReader["MS_ID"].ToString();
            model.CauseGroupID = dataReader["CauseGroupID"].ToString();
            model.SituationID = dataReader["SituationID"].ToString();
            ojb = dataReader["IsMatch"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsMatch = (int)ojb;
            }
            model.MatchSource = dataReader["MatchSource"].ToString();
            model.AffirmIdeaSource = dataReader["AffirmIdeaSource"].ToString();
            model.AffirmIdeaResult = dataReader["AffirmIdeaResult"].ToString();

            model.ParentID = dataReader["ParentID"].ToString();
            model.AllParentID = dataReader["AllParentID"].ToString();
            model.AllStituationIDs = dataReader["AllStituationIDs"].ToString();

            ojb = dataReader["SituationType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SituationType = (int)ojb;
            }
            return model;
        }
        #endregion  Method
    }
}
